package com.ruoyi.teaching.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaClassMapper;
import com.ruoyi.teaching.domain.YaClass;
import com.ruoyi.teaching.service.IYaClassService;

/**
 * 班级Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-01
 */
@Service
public class YaClassServiceImpl implements IYaClassService {
  @Autowired
  private YaClassMapper yaClassMapper;

  /**
   * 查询班级
   *
   * @param id 班级主键
   * @return 班级
   */
  @Override
  public YaClass selectYaClassById(Long id) {
//        return yaClassMapper.selectYaClassById(id);
    return yaClassMapper.selectById(id);
  }

  /**
   * 查询班级列表
   *
   * @param yaClass 班级
   * @return 班级
   */
  @Override
  public List<YaClass> selectYaClassList(YaClass yaClass) {
//        return yaClassMapper.selectYaClassList(yaClass);
    QueryWrapper<YaClass> wrapper = new QueryWrapper<>();
    // 使用数据库实际字段名而不是Java属性名
    if (yaClass.getClassName() != null && !yaClass.getClassName().isEmpty()) {
      wrapper.like("class_name", yaClass.getClassName());
    }
    if (yaClass.getRemarks() != null && !yaClass.getRemarks().isEmpty()) {
      wrapper.like("remarks", yaClass.getRemarks());
    }
    if (yaClass.getBeginDate() != null && !yaClass.getBeginDate().isEmpty()) {
      wrapper.eq("begin_date", yaClass.getBeginDate());
    }
    if (yaClass.getRestDay() != null) {
      wrapper.eq("rest_day", yaClass.getRestDay());
    }
    if (yaClass.getSelfStudyDay() != null) {
      wrapper.eq("self_study_day", yaClass.getSelfStudyDay());
    }
    if (yaClass.getType() != null && !yaClass.getType().isEmpty()) {
      wrapper.eq("type", yaClass.getType());
    }
    return yaClassMapper.selectList(wrapper);
  }

  /**
   * 新增班级
   *
   * @param yaClass 班级
   * @return 结果
   */
  @Override
  public int insertYaClass(YaClass yaClass) {
    convertDateStringsToDatabaseFormat(yaClass);
    yaClass.setCreateTime(DateUtils.getNowDate());
//        return yaClassMapper.insertYaClass(yaClass);
    return yaClassMapper.insert(yaClass);
  }

  /**
   * 修改班级
   *
   * @param yaClass 班级
   * @return 结果
   */
  @Override
  public int updateYaClass(YaClass yaClass) {
    // 在更新前进行数据转换
    convertDateStringsToDatabaseFormat(yaClass);
    yaClass.setUpdateTime(DateUtils.getNowDate());
//        return yaClassMapper.updateYaClass(yaClass);
    return yaClassMapper.updateById(yaClass);
  }

  /**
   * 批量删除班级
   *
   * @param ids 需要删除的班级主键
   * @return 结果
   */
  @Override
  public int deleteYaClassByIds(Long[] ids) {
//        return yaClassMapper.deleteYaClassByIds(ids);
    return yaClassMapper.deleteByIds(Arrays.asList(ids));
  }

  /**
   * 删除班级信息
   *
   * @param id 班级主键
   * @return 结果
   */
  @Override
  public int deleteYaClassById(Long id) {
//        return yaClassMapper.deleteYaClassById(id);
    return yaClassMapper.deleteById(id);
  }

  /**
   * 将日期字符串转换为数据库格式
   *
   * @param yaClass
   */
  private void convertDateStringsToDatabaseFormat(YaClass yaClass) {
    // 如果restDayStr不为空，将其转换为适合数据库存储的格式
    if (yaClass.getRestDay() != null) {
      // 根据数据库列的实际类型进行转换
      // 如果数据库列是DATE类型，可以直接使用字符串
      // 如果是数值类型，需要转换
      yaClass.setRestDay(convertDateStringToDatabaseValue(yaClass.getRestDay().toString()));
    }

    // 如果selfStudyDayStr不为空，将其转换为适合数据库存储的格式
    if (yaClass.getSelfStudyDay() != null) {
      yaClass.setSelfStudyDay(convertDateStringToDatabaseValue(yaClass.getSelfStudyDay().toString()));
    }
  }

  /**
   * 转换日期字符串为数据库值
   *
   * @param dateStr
   * @return
   */
  private Integer convertDateStringToDatabaseValue(String dateStr) {
    // 根据数据库列的实际类型实现转换逻辑
    // 这里假设数据库列是INT类型，需要存储为数值
    try {
      // 示例转换逻辑，您需要根据实际情况调整
      return Integer.parseInt(dateStr.replaceAll("-", ""));
    } catch (Exception e) {
      return null;
    }
  }

  /**
   * 根据班级名称查询班级信息
   *
   * @param className
   * @return
   */
  @Override
  public YaClass selectYaClassByClassName(String className) {
    return yaClassMapper.selectYaClassByClassName(className);
  }
}
